<?php
namespace Store\Controller;
use Think\Image;

use Think\Page;

use Think\Controller;

use Common\Controller\AuthController;
class ScenicController extends AuthController {
	public function index(){
		$scenic = M("Scenic");
        $userid = $_SESSION['uid'];
		$count = $scenic->where(array('user_id'=>$userid,'status'=>0))->count();
		$page = new Page($count,4);
		$page->setConfig('prev', '上页');
		$page->setConfig('next', '下页');
		$show = $page->show();
		$list = $scenic->where(array('user_id'=>$userid,'status'=>0))->limit($page->firstRow,$page->listRows)->select();

		$this->assign('list',$list);
		$this->assign('count',$count);
		$this->assign('page',$show);
		$this->display();
	}
	public function add(){
		if(IS_POST){
			$file = $_FILES['image'];
			$uploadpath = "Public/Upload/Scenic/";//上传路径

			$util = A('Util');
			$util->createDir($uploadpath);//上传路径是否已经存在，不存在则创建

			$pinfo = pathinfo($file['name']);
			$ftype = $pinfo['extension'];//文件类型
			$name = basename($file['name'],".{$ftype}");

			$fileName = md5($name.time()).".".$ftype;//识别图
			$image = new Image();
			$image->open($file['tmp_name']);
			$image->save($uploadpath.$fileName);

			$data = $_POST;
			$data["image"] = "http://qianmosen.cn/tubu/".$uploadpath.$fileName;
			$data["user_id"] = $_SESSION['uid'];
				
			$scenic = D('Scenic');
			$data = $scenic->create($data);
			$result = $scenic->add($data);
			if($result){
				$this->success("添加成功","index");
			}else{
				$this->error("添加失败");
			}
		}else{
			$this->display();
		}
	}
	public function delete(){
		$where['id'] = $_GET['id'];
		$scenic = D('Scenic');
		$list = $scenic->where($where)->find();
		$vv   = substr($list['image'], 25);
		if(is_file($vv)){
			unlink($vv);
		}
		$ticket = D('ScenicTicket');
		$where2['scenic_id'] = $_GET['id'];
		$arr = $ticket->where($where2)->select();
		foreach ($arr as $item){
			$v = substr($item['image'], 25);
			if(is_file($v)){
				unlink($v);
			}
		}
		$ticket->where($where2)->delete();
		$result = $scenic->where($where)->delete();
		if($result){
			$this->success("删除成功","index");
		}else{
			$this->error("删除失败");
		}
	}

	public function editType(){
		$sid = I('sid');
		$scenicName = M('Scenic')->where(array('id'=>$sid))->getField('name');
		$scenicType = M('Scenic')->where(array('id'=>$sid))->getField('type');
		$image      = M('Scenic')->where(array('id'=>$sid))->getField('image');
		if(strstr($scenicType,'古村游')){
			$gucun = '古村游';
		}
		if(strstr($scenicType,'特惠游')){
			$tehui  = '特惠游';
		}
		if(strstr($scenicType,'活动游')){
			$huodong = '活动游';
		}
		if(strstr($scenicType, '山水游')){
			$shanshui = '山水游';
		}
		
		$this->assign('gucun',$gucun);
		$this->assign('tehui',$tehui);
		$this->assign('shanshui',$shanshui);
		$this->assign('huodong',$huodong);
		$this->assign('image',$image);
		
		$this->assign('scenicName',$scenicName);
		$this->display();
	}
	public function change(){
		$file = $_FILES['newimg'];
	//	file_put_contents("Public/Log/log.txt", "post:".json_encode($_POST)."\r\n",FILE_APPEND|LOCK_EX);
	//	file_put_contents("Public/Log/log.txt", "file:".json_encode($_FILES)."\r\n",FILE_APPEND|LOCK_EX);
		if($file['size']>0){
			$where['name'] = I('scenicname');
			$scenic = D('Scenic');
			$list = $scenic->where($where)->find();
			$vv   = substr($list['image'], 25);
			if(is_file($vv)){
				unlink($vv);
			}
			$uploadpath = "Public/Upload/Scenic/";//上传路径
			
			$util = A('Util');
			$util->createDir($uploadpath);//上传路径是否已经存在，不存在则创建
			
			$pinfo = pathinfo($file['name']);
			$ftype = $pinfo['extension'];//文件类型
			$name = basename($file['name'],".{$ftype}");
			
			$fileName = md5($name.time()).".".$ftype;//识别图
			$image = new Image();
			$image->open($file['tmp_name']);
			$image->save($uploadpath.$fileName);
			
			$data["image"] = "http://qianmosen.cn/tubu/".$uploadpath.$fileName;
		}
		$data['name'] = I('scenicname');
		$data['type'] = I('str');
		if(M('Scenic')->where(array('name'=>$data['name']))->save($data)){
			$this->ajaxReturn(array('status'=>'1','imgurl'=>$data["image"]));
		}
	}
	
}